
		$(document).ready(function(){
			var table=$("#crmuserTable");
			var keyword="";	
			var t=table.dataTable({
				"ajax":function(data,callback,setting){
					var page = table.api().page.info();
					$.post("/index.php?m=Admin&c=CrmUser&a=search",{pageIndex:page.page+1,pageSize:page.length,select:keyword},function(data,status){
						console.log(data);
							callback(data);	
					});
				},
 				"pageLength":10,
  				"ordering":false,
          "deferRender": true,
  				"columns":[
  				{"data":"id"},
          {"data":"id"},
  				{"data":"user_name"},
  				{"data":"real_name"},
  				{"data":"sex"},
          {"data":"mobile"},
          {"data":"role"},
  				{"data":"oaUser"},//是否绑定OA
  				{"data":"is_active"},
          {"data":"id"},
  				],
  				"columnDefs":[
  				{
  					"targets":0,
  					"data":"id",
  					"render":function(data,type,full,meta){
  						return '<input type="checkbox" value="'+data+'" name="crmuser">';
  					}

  				},
          {
            "targets":4,
            "data":"sex",
            "render":function(data,type,full,meta){
              var str="";
              switch(data){
                case "M":
                    str="男";
                    break;
                case "F":
                    str='女';
                    break;
              }
              return str;
            }
          },
           {
            "targets":6,
            "data":"Role",
            "render":function(data,type,full,meta){
              if(data.length!==0){
                var str='';
                for(x in data){
                  if(x==data.length-1){
                       str+=data[x].name;
                  }else{
                       str+=data[x].name+'，';
                  }
                }
                return str;
              }else{
                return "";
              }

            }
          },
          {
            "targets":7,
            "data":"oaUser",
            "render":function(data,type,full,meta){
              var str="";

                if(data.length==0){
                  str='<span class="label radius">未绑定</span>';
                }else{
                  str='<span class="label label-success radius">已绑定</span>';
                }

                return str;

            }

          },
  				{
  					"targets":8,
  					"data":"status",
  					"render":function(data,type,full,meta){
  						var str="";
  						switch(data){
  							case "0":
  							str='<span class="label radius state">已停用</span>';
  							break;
  							case "1":
  							str='<span class="label label-success radius state">已启用</span>';
  							break;
  						}
  						return str;
  					}
  				},
          
  				{
  					"targets":9,
  					"data":"id",
  					"render":function(data,type,full,meta){
              if(full.oaUser.length==0){
                var str='<a class="label label-primary radius  oaAddBtn buid-'+data+'" uid="'+data+'" href="javascript:;">绑定OA账户</a><a style="text-decoration:none"  href="javascript:;" title="启用" class="start" uid='+data+'><i class="Hui-iconfont"></i></a><a title="编辑" href="javascript:;" uid="'+data+'" class="ml-5 crmuserEdit" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a><a title="删除" href="javascript:;" uid="'+data+'" class="ml-5 crmuserDel uid-'+data+'" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a>';
              }else{
                var uid="";
                for(x in full.oaUser){
                    if(x==full.oaUser.length-1){
                      uid+=full.oaUser[x].UID;
                    }else{
                       uid+=full.oaUser[x].UID+',';
                    }
                }
                var str='<a class="label label-warning radius oaDelBtn buid-'+data+'" cid="'+data+'" uid="'+uid+'" href="javascript:;">解绑OA账户</a><a style="text-decoration:none"  href="javascript:;" title="启用" class="start" uid='+data+'><i class="Hui-iconfont"></i></a><a title="编辑" href="javascript:;" uid="'+data+'" class="ml-5 crmuserEdit" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a><a title="删除" href="javascript:;" uid="'+data+'" class="ml-5 crmuserDel uid-'+data+'" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a>';
              }
  						
  						return str;
  					}

  				},
  				],
  			"fnCreatedRow":function(row,data,index){
  				//row 表示当前tr，data表示当前行的元素对象，index表示当前tr索引
  				var status=$(row).find(".state").text();
          if(status=='已启用'){
            $(row).find(".start").attr("title",'停用');
            $(row).find(".start i").addClass('Hui-iconfont-shenhe-tingyong')
          }else{
             $(row).find(".start").attr("title",'启用');
              $(row).find(".start i").addClass('Hui-iconfont-gouxuan');
          }
  			},
  			
			"bPaginate": true, //翻页功能
			"searching":false,
			// "processing": true,
        	"serverSide": true,//服务器模式
        			// 	"infoCallback": function( settings, start, end, max, total, pre ) {
  //  				table.DataTable().ajax.reload();
  // },

        });
		$(".searchButton").click(function(){
			keyword=$("#searchName").val();
			t.api().draw();
		});
		// 回车事件
		document.onkeydown = function(e){
                 var ev = document.all ? window.event : e;
                 if(ev.keyCode==13) {
                    keyword=$("#searchName").val();
                    t.api().draw();
                 }
            }
	});